package com.webchat.ugc.service.callback.payment;


import com.webchat.common.constants.RedPacketConstants;
import com.webchat.common.enums.payment.PaymentBusinessActionEnum;
import com.webchat.common.exception.BusinessException;
import com.webchat.ugc.service.mall.MallProductOrderService;
import com.webchat.ugc.service.redpacket.AbstractOpenRedPacketService;
import com.webchat.ugc.service.redpacket.OpenRedPacketWithLuaService;
import com.webchat.ugc.service.redpacket.OpenRedPacketWithQueueService;
import com.webchat.ugc.service.redpacket.OpenRedPacketWithRedissonLockService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 支付callback处理工厂
 *
 * 抽象工厂设计模式
 */
@Component
public class PaymentCallbackFactory implements InitializingBean, ApplicationContextAware {

    private ApplicationContext applicationContext;

    public static Map<String, AbstractPaymentCallback> services = new ConcurrentHashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        this.initServices();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void initServices() {

        /**
         * 注册电商商品订单支付回调处理服务
         */
        services.put(PaymentBusinessActionEnum.MALL_PRODUCT_ORDER.getAction(),
                applicationContext.getBean(MallProductOrderService.class));
    }

    public static AbstractPaymentCallback getService(String action) {
        AbstractPaymentCallback abstractPaymentCallback = services.get(action);
        if (abstractPaymentCallback == null) {
            throw new BusinessException("不支持支付回调处理");
        }
        return abstractPaymentCallback;
    }
}
